package stark.app.function.idiom;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import stark.app.R;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;

/**
 * Author：daizhongbin on 2022/7/11
 * Email：1027154338@qq.com
 * Desc:
 */
public class IdiomXxAdapter extends StkProviderMultiAdapter<String> {

    public IdiomXxAdapter() {
        super(1);
        addItemProvider(new MyItemProvider());
    }

    public void initData(int count) {
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            datas.add("");
        }
        setNewInstance(datas);
    }

    public List<View> getNextEmptyIdiomViewList() {
        List<String> datas = getData();
        if (datas == null || datas.size() == 0) {
            return null;
        }
        int i;
        for (i = 0; i < datas.size(); i++) {
            if (TextUtils.isEmpty(datas.get(i))) {
                break;
            }
        }
        RecyclerView.ViewHolder holder = getRecyclerView().findViewHolderForAdapterPosition(i);
        if (holder == null) {
            return null;
        }
        List<View> views = new ArrayList<>();
        views.add(holder.itemView.findViewById(R.id.tvChar1));
        views.add(holder.itemView.findViewById(R.id.tvChar2));
        views.add(holder.itemView.findViewById(R.id.tvChar3));
        views.add(holder.itemView.findViewById(R.id.tvChar4));
        return views;
    }

    public void addIdiom(String idiom) {
        List<String> datas = getData();
        if (datas == null || datas.size() == 0) return;
        int i;
        for (i = 0; i < datas.size(); i++) {
            if (TextUtils.isEmpty(datas.get(i))) {
                break;
            }
        }
        datas.set(i, idiom);
        notifyItemChanged(i);
    }

    /**
     * 检测游戏是否完成
     *
     * @return
     */
    public boolean checkComplete() {
        List<String> datas = getData();
        if (datas == null || datas.size() == 0) {
            return true;
        }
        for (String data : datas) {
            if (TextUtils.isEmpty(data)) {
                return false;
            }
        }
        return true;
    }

    private class MyItemProvider extends BaseItemProvider<String> {

        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_idiom_xx_char;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, String idiom) {
            TextView tvChar1 = holder.getView(R.id.tvChar1);
            TextView tvChar2 = holder.getView(R.id.tvChar2);
            TextView tvChar3 = holder.getView(R.id.tvChar3);
            TextView tvChar4 = holder.getView(R.id.tvChar4);

            if (TextUtils.isEmpty(idiom)) {
                setViewChar(tvChar1, "");
                setViewChar(tvChar2, "");
                setViewChar(tvChar3, "");
                setViewChar(tvChar4, "");
            } else {
                setViewChar(tvChar1, idiom.substring(0, 1));
                setViewChar(tvChar2, idiom.substring(1, 2));
                setViewChar(tvChar3, idiom.substring(2, 3));
                setViewChar(tvChar4, idiom.substring(3, 4));
            }
        }

        private void setViewChar(TextView textView, String strChar) {
            textView.setSelected(TextUtils.isEmpty(strChar) ? false : true);
            textView.setText(strChar);
        }
    }

}
